Subscript a Data Frame Object

DESCRIPTION:
Allows the user to extract values from a data frame object by using subscripts.

USAGE:
x[i, j, drop]
x[i, j] <- value
x[[i, j]]
x[[i, j]] <- value

REQUIRED ARGUMENTS:
x:
an object inheriting from class "data.frame".

OPTIONAL ARGUMENTS:
i, j:
subscripts applying to the rows and columns (variables) respectively. Either can be omitted. So long as the subscripting looks matrix-like, the method treats x as a matrix. However, if non-matrix subscripting is done; that is, x[j]; x[[j]] then x is treated as a frame or list; and the index j is assumed to index the variables of the data frame.
drop:
optional flag to control dropping of individual rows or columns. By default, single columns are dropped to the corresponding variable, but single rows remain data frames.
value:
replacement value for the relevent piece of the data frame. This can, but need not, be a data frame itself. If you are replacing data in more than one column of a data frame, and the columns differ in the kind of data (e.g., some are numeric, others factor), it will generally be much more natural and less error-prone to construct the replacement data as a data frame with the appropriate replacement variables in the columns. So far as the method is concerned, however, the right-hand side can also be an atomic vector or a list. In the case that one complete variable is being replaced, the value can be anything that would make sense as that variable.

VALUE:
the data frame formed by extracting or replacing the relevant rows and/or columns. In the case of x[i,] or x[,j] selecting a single row or column, the extracted data may be dropped from a data frame to a vector, following the rules above.

HINTS:
It's important to distinguish x[[j]], which selects a single variable as a vector, from x[[i, j]] selecting a single entry. Also, if you do really want to select single variables from a data frame (for example, in a loop), x[[j]] is substantially faster than x[,j]. The latter expression uses extra calculations to preserve the row names and other data frame structure as it selects columns; if this structure is irrelevant to your application, extract elements treating the data frame as a list. When replacing a column, however, the extra computation may be worthwhile. Assigning one or more columns of a data frame will check that the new values have the right number of rows and generally try to ensure that the assignment leaves the data frame in a valid state.

Notice too that the use of x[[j]] is one place that data frames differ from ordinary S matrices. The k-th element of x in the matrix sense would be defined by the S rule of matrix element ordering as x[[i, j]] where i <- 1 + mod(k-1, r); j <- ceiling(k/r) and r is the number of rows of the matrix.


SEE ALSO:
Subscript , data.frame . attach.data.frame for an alternative when modifying a data frame.

EXAMPLES:
solder[sample(900,10), ]
attach(fuel.frame); fuel.frame[, "h"] <- Weight/Disp.